package LandR.login.loginSrc.function.Help;


import LandR.login.loginSrc.function.User.User;

import java.sql.*;

public class MysqlFunctionHelpUtil {
    /**
     * 这个类有的是一些与注册相关的连接数据库的方法
     */
    //1.getConnection 获取数据库连接的方法，返回一个Connection对象
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        final String URL = "jdbc:mysql://123.57.3.195:3306/YDYP?characterEncoding=utf8&useSSL=true&serverTimezone=UTC";
        final String username = "root";
        final String password = "ZAMTYR1314";
        Connection conn = DriverManager.getConnection(URL, "root", "ZAMTYR1314");
        if (conn == null) {
            System.out.println("创建连接失败");
            close(conn);
            return null;
        } else {
            System.out.println("创建连接成功");
            return conn;
        }

    }

    //2.getPrepareStatement  获取PrepareStatement接口方法
    public static PreparedStatement getPrepareStatement(String sql) throws SQLException, ClassNotFoundException {
        Connection conn = getConnection();
        PreparedStatement ppsm = (PreparedStatement) conn.prepareStatement(sql);
        return ppsm;
    }

    //3.关闭prepareStatement接口的方法
    public static void close(PreparedStatement pstmt) {
        if (pstmt != null) {                        //避免出现空指针异常
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

    //4.关闭connection接口的方法
    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }

    //5.关闭resultSet接口的方法
    public static void close(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }

    //6.根据用户名查看数据库中用户的方法,如果包含用户则返回包含id的user对象，如果不包含返回id为-1的user对象
    public User selectUserFromDataByUsername(User user) {
        System.out.println("正在进行用户名密码登录");
        Connection conn = null;
        PreparedStatement ppst = null;
        ResultSet rst = null;

        Integer result = -1;
        try {
            String username = user.getUsername();
            String passward = user.getPassword();
            conn = getConnection();
            String sql = "select * from users where username = ? ";
            ppst = getPrepareStatement(sql);
            ppst.setString(1, username);
            System.out.println("username上传成功");
            rst = (ResultSet) ppst.executeQuery();
            while (rst.next()) {
                String psw = rst.getString("password");
                System.out.println("信息获取成功");
                if (psw.equals(passward)) {
                    result = rst.getInt("id");
                    System.out.println("用户信息正确");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            close(rst);
            close(ppst);
            close(conn);
        }
        user.setId(result);
        System.out.println("用户信息错误登录失败");
        return user;
    }

    //7.根据邮箱查看数据库中用户的方法,如果包含用户则返回包含id的user对象，如果不包含返回id为-1的user对象
    public User selectUserFromDataByemail(User user) {
        System.out.println("正在进行邮箱密码登录");
        Connection conn = null;
        PreparedStatement ppst = null;
        ResultSet rst = null;

        Integer result = -1;
        try {
            String email = user.getEmail();
            String passward = user.getPassword();
            conn = getConnection();
            String sql = "select * from users where email = ? ";
            ppst = getPrepareStatement(sql);
            ppst.setString(1, email);
            System.out.println("username上传成功");
            rst = (ResultSet) ppst.executeQuery();
            while (rst.next()) {
                String psw = rst.getString("password");
                System.out.println("信息获取成功");
                if (psw.equals(passward)) {
                    result = rst.getInt("id");
                    System.out.println("用户信息正确");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            close(rst);
            close(ppst);
            close(conn);
        }
        user.setId(result);
        System.out.println("用户信息错误登录失败");
        return user;
    }

    //7.根据手机号查看数据库中用户的方法,如果包含用户则返回包含id的user对象，如果不包含返回id为-1的user对象
    public User selectUserFromDataByePhoneNumber(User user) {
        System.out.println("正在进行手机号密码登录");
        Connection conn = null;
        PreparedStatement ppst = null;
        ResultSet rst = null;

        Integer result = -1;
        try {
            String phoneNumber = user.getPhoneNumber();
            String passward = user.getPassword();
            conn = getConnection();
            String sql = "select * from users where phonenumber = ? ";
            ppst = getPrepareStatement(sql);
            ppst.setString(1, phoneNumber);
            System.out.println("username上传成功");
            rst = (ResultSet) ppst.executeQuery();
            while (rst.next()) {
                String psw = rst.getString("password");
                System.out.println("信息获取成功");
                if (psw.equals(passward)) {
                    result = rst.getInt("id");
                    System.out.println("用户信息正确");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            close(rst);
            close(ppst);
            close(conn);
        }
        user.setId(result);
        System.out.println("用户信息错误登录失败");
        return user;
    }

    //8.选择通过邮箱登录/手机号登录/用户名登录的方法
    //返回1用户名登录，返回2手机号登录，返回3邮箱登录
    public Integer judgeMethodForRegister(String name) {
        char[] names = name.toCharArray();
        int result = 0;
        if (name.contains("@") && name.contains(".com")) {
            System.out.println("即将进行邮箱登录登录");
            result = 3;
        } else if (names.length == 11) {
            for (char c : names) {
                if (c <= '9' && c >= 0) {
                    System.out.println("即将进行手机号登录");
                    result = 2;
                } else {
                    System.out.println("即将进行用户名登录");
                    result = 1;
                }
            }
        } else {
            System.out.println("即将进行用户名登录");
            result = 1;
        }
        return result;
    }
}







